package com.shengqin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.system.SystemUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.shengqin.config.CommonException;
import com.shengqin.service.IFileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Service
public class FileServiceImpl implements IFileService {
    @Value("${upload.path}")
    private String uploadPath;
    @Value("${upload.domain}")
    private String uploadDomain;

    @Override
    public String storageFileWithReturnUrl(String bucketName, MultipartFile multipartFile) {
        String fileId = IdWorker.getIdStr();
        String finalFileKey = genFileKey(fileId, multipartFile);
        storageFile(bucketName, finalFileKey, multipartFile);
        return getFileAuthUrl(bucketName, finalFileKey);
    }

    private void initClient() {
        if(!FileUtil.exist(uploadPath)) {
            FileUtil.mkdir(uploadPath);
        }

    }

    public String genFileKey(String fileId, MultipartFile file) {

        // 获取文件原始名称
        String originalFileName = file.getOriginalFilename();

        // 获取文件后缀
        String fileSuffix = FileUtil.getSuffix(originalFileName);

        // 生成文件的对象名称，格式如:1377109572375810050.docx
        String fileObjectName = fileId + StrUtil.DOT + fileSuffix;

        // 获取日期文件夹，格式如，2021/10/11/
        String dateFolderPath = DateUtil.thisYear() + StrUtil.SLASH +
                (DateUtil.thisMonth() + 1) + StrUtil.SLASH +
                DateUtil.thisDayOfMonth() + StrUtil.SLASH;

        // 返回
        return dateFolderPath + fileObjectName;
    }

    public void storageFile(String bucketName, String key, MultipartFile multipartFile) {
        InputStream inputStream;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            throw new CommonException("获取文件流异常，名称是：{}", multipartFile.getName());
        }
        storageFile(bucketName, key, inputStream);
    }

    public void storageFile(String bucketName, String key, InputStream inputStream) {
        initClient();
        FileUtil.writeFromStream(inputStream, uploadPath + FileUtil.FILE_SEPARATOR + bucketName + FileUtil.FILE_SEPARATOR + key);
    }

    public String getFileAuthUrl(String bucketName, String key) {
        return uploadDomain + FileUtil.FILE_SEPARATOR + bucketName + FileUtil.FILE_SEPARATOR + key;
    }

    public File getFileByBucketNameAndKey(String bucketName, String key) {
        initClient();
        String path = uploadPath + FileUtil.FILE_SEPARATOR + bucketName + FileUtil.FILE_SEPARATOR + key;
        File file = FileUtil.file(path);
        if(!FileUtil.exist(file)) {
            throw new CommonException("文件{}不存在", path);
        }
        return file;
    }
}
